/**
 * Created by kjmk on 2015/7/9.
 */

module.exports = Controller("Test/BaseController", function(){
    "use strict"
    var wsList = {};
    setInterval(function(){
        for(var id in wsList){
            wsList[id].send({
                name: '机器人',
                text: '每隔10秒我就发一条消息哦'
            })
        }
    }, 10000);
    return {
        indexAction: function(){
            this.display();
        },
        testwsAction: function(){
            console.log(wsList);
            this.display();
        },
        wsopenAction: function(){
            var socket = this.http.websocket;
            var id = socket.id;
            wsList[id] = socket;

            this.http.on("websocket.close", function(){
                console.log('close')
                delete wsList[id];
                for(var wid in wsList){
                    wsList[wid].send({
                        name: 'id_' + id,
                        text: 'goodbye~~'
                    });
                }
            })
        },
        wsmessageAction: function () {
            var data = this.get();
            for(var wid in wsList){
                wsList[wid].send(data);
            }
        }
    }
})
